/*
 * Copyright (c) 2009 Sun Microsystems, Inc.
 * Copyright (c) 2011 Oracle Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **/

package com.sun.spot.wot.gateway;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

/**
 *
 * @author vgupta
 */
public class TCP6Forwarder extends StreamForwarder {
    Socket conn = null;
    StreamConnection sconn = null;
    public void openConn(String addr) {
        try {
            String[] parts = addr.split(":");
//            String[] parts = {
//                "fe80:0:0:0:0214:4f01:0000:5317",
//                "8888"
//            };
            if (parts.length == 1) {
//                System.out.println("Opening socket on " + addr + "...");
                conn = new Socket(addr, 80);
                is = conn.getInputStream();
                os = conn.getOutputStream();
            } else if (parts.length == 2) {
//                System.out.println("Opening socket on " + addr + "...");
                conn = new Socket(parts[0], Integer.parseInt(parts[1]));
                is = conn.getInputStream();
                os = conn.getOutputStream();
            } else {
                System.out.println("Opening tcp://" + addr + "...");
                sconn = (StreamConnection) Connector.open("tcp://" + addr);
                is = sconn.openInputStream();
                os = sconn.openOutputStream();
            }
        } catch (UnknownHostException ex) {
            Logger.getLogger(TCP6Forwarder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TCP6Forwarder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NumberFormatException ex) {
            Logger.getLogger(TCP6Forwarder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void closeConn() {
        try {
            if (conn != null) {
                conn.close();
            }
            if (sconn != null) {
                sconn.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(TCP6Forwarder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
